﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Xml;
namespace GoShopping.Web.ProductManager
{
    public partial class AddSuplierInfo : System.Web.UI.Page
    {
        BLL.ProductSuplyBean psb = new GoShopping.BLL.ProductSuplyBean();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.ElementSelectedChange = GetTreeView;
                GetGVData();
            }
        }

        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
            //Master.MasterTreeView = tv;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            // 使用ArrayList记录选择的编号
            ArrayList selList = new ArrayList();
            int i = 0;
            // 遍历数据集
            foreach (GridViewRow row in this.extGVSuplier.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                //// 若为Checked，将信息添加到ArrayList
                selList.Add(((Label)row.FindControl("lblID")).Text);
                i++;
            }
            // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
            if (selList.Count == 0)
            {
                return;
            }
            // 调用逻辑方法，执行删除操作
            BLL.ProductSuplyBean psb = new GoShopping.BLL.ProductSuplyBean();
            for (int j = 0; j < selList.Count; j++)
            {
                try
                {
                    psb.DelPrdctSuply(selList[j].ToString());
                }
                catch
                {
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('删除失败,相关数据没有删除!')</script>", false);
                }
                psb.DelPrdctSuply(selList[j].ToString());
            }
            // 重新绑定列表
            GetGVData();
        }

        private IList<GoShopping.Model.PrdctSupplyer> GetGVData()
        {
            IList<GoShopping.Model.PrdctSupplyer> lPs = psb.GetAllProductSuplyList();
            extGVSuplier.DataSource = lPs;
            extGVSuplier.DataBind();
            return lPs;
        }

        protected void extGVSuplier_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "psEdit")
            {
                int id = Convert.ToInt16(e.CommandArgument);
                Model.PrdctSupplyer ps = new Model.PrdctSupplyer();
                ps = psb.GetProductSuply(id);

                hidSupplier.Value = ps.ID.ToString();
                zlTxtSupplier.Text = ps.Name;
                zlTxtFullCompName.Text = ps.CompanyFullName;
                zlTxtPhone.Text = ps.Telephone;
                zlTxtAddress.Text = ps.Address;
                zlTxtRemark.Text = ps.Remark;
            }
        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.extGVSuplier.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.PrdctSupplyer ps = new GoShopping.Model.PrdctSupplyer();
            ps.Name = zlTxtSupplier.Text.Trim();
            ps.CompanyFullName = zlTxtFullCompName.Text.Trim();
            ps.Telephone = zlTxtPhone.Text.Trim();
            ps.Address = zlTxtAddress.Text.Trim();
            ps.Remark = zlTxtRemark.Text.Trim();
            try
            {
                if (psb.AddPrdctSuply(ps))
                {
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('保存成功!')</script>", false);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('已经存在!')</script>", false);
                }
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('保存失败!')</script>", false);
            }
            GetGVData();
        }

        protected void btnClean_Click(object sender, EventArgs e)
        {
            zlTxtSupplier.Text = "";
            zlTxtFullCompName.Text = "";
            zlTxtPhone.Text = "";
            zlTxtAddress.Text = "";
            zlTxtRemark.Text = "";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Model.PrdctSupplyer ps = new GoShopping.Model.PrdctSupplyer();
            ps.ID = Convert.ToInt16(hidSupplier.Value);
            ps.Name = zlTxtSupplier.Text.Trim();
            ps.CompanyFullName = zlTxtFullCompName.Text.Trim();
            ps.Telephone = zlTxtPhone.Text.Trim();
            ps.Address = zlTxtAddress.Text.Trim();
            ps.Remark = zlTxtRemark.Text.Trim();
            try
            {
                psb.UpdatePrdctSuply(ps);
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('修改成功!')</script>", false);
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('修改失败!')</script>", false);
            }
            GetGVData();
        }

        protected void extGVSuplier_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVSuplier.PageIndex = e.NewPageIndex;
            GetGVData();
        }
    }
}
